/**
 * CatalogPieceOfProductPane.java 22 juil. 07
 * 
 * Copyright (c) 2007 windu.2b <jcaddie@windu2b.org>. All Rights Reserved.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.windu2b.jcaddie.swing;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;
import javax.swing.event.*;

import org.windu2b.jcaddie.model.*;

/**
 * @author windu.2b
 * 
 */
public class CatalogPieceOfProductPane extends JPanel
{
	private static final long	            serialVersionUID	= 1945094177173560589L;


	private CatalogPieceOfProduct	        piece;


	private UserPreferences	                preferences;


	private ResourceBundle	                resource;


	private CatalogPieceOfProductController	controller;


	private CardLayout	                    cardLayout;


	private JLabel	                        categoryNameLabel;


	private JButton	                        categoryChangeButton;


	private JLabel	                        productNameLabel;


	private JLabel	                        productPoidsLabel;


	private JList	                        productUnitList;




	/**
	 * @param catalog
	 * @param preferences
	 * @param controller
	 */
	public CatalogPieceOfProductPane( CatalogPieceOfProduct piece,
	        UserPreferences preferences,
	        CatalogPieceOfProductController controller )
	{
		this.piece = piece;
		this.preferences = preferences;
		this.resource = ResourceBundle
		        .getBundle( CatalogPieceOfProductPane.class.getName() );
		this.controller = controller;

		createComponents( preferences );
		setMnemonics();
		layoutComponents();
	}




	/**
	 * Displays this panel in a JFrame.
	 */
	public void displayView()
	{
		System.out.println( "jframe" );
		JFrame f = new JFrame( "Produit :" + piece );
		f.add( this );
		f.pack();
		f.setVisible( true );
	}




	/**
	 * Sets components mnemonics and label / component associations.
	 */
	private void setMnemonics()
	{
		if ( !System.getProperty( "os.name" ).startsWith( "Mac OS X" ) )
		{
			System.out.println( categoryNameLabel );
			this.categoryNameLabel
			        .setDisplayedMnemonic( KeyStroke
			                .getKeyStroke(
			                        this.resource
			                                .getString( "categoryNameLabel.mnemonic" ) )
			                .getKeyCode() );
			this.categoryNameLabel
			        .setLabelFor( this.categoryChangeButton );
		}
	}




	/**
	 * Layouts components in 3 panels added to this panel as cards.
	 */
	private void layoutComponents()
	{
		this.cardLayout = new CardLayout();
		setLayout( this.cardLayout );

		JPanel CategoryInformationPanel = new JPanel( new GridBagLayout() );
		CategoryInformationPanel.add( this.categoryNameLabel,
		        new GridBagConstraints( 0, 0, 1, 1, 0, 0,
		                GridBagConstraints.WEST, GridBagConstraints.NONE,
		                new Insets( 5, 0, 5, 0 ), 0, 0 ) );
		CategoryInformationPanel.add( this.categoryChangeButton,
		        new GridBagConstraints( 0, 1, 1, 1, 0, 0,
		                GridBagConstraints.CENTER, GridBagConstraints.NONE,
		                new Insets( 0, 0, 0, 0 ), 0, 0 ) );
		/*
		 * CategoryInformationPanel.add( this.imageChoiceErrorLabel, new
		 * GridBagConstraints( 0, 2, 1, 1, 0, 0, GridBagConstraints.WEST,
		 * GridBagConstraints.NONE, new Insets( 5, 0, 0, 0 ), 0, 0 ) );
		 * CategoryInformationPanel.add( this.imageChoicePreviewComponent, new
		 * GridBagConstraints( 0, 3, 1, 1, 1, 1, GridBagConstraints.CENTER,
		 * GridBagConstraints.NONE, new Insets( 5, 0, 5, 0 ), 0, 0 ) );
		 */

		JPanel productPanel = new JPanel( new GridBagLayout() );
		productPanel.add( this.productNameLabel, new GridBagConstraints( 0, 0,
		        2, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE,
		        new Insets( 5, 0, 5, 0 ), 0, 0 ) );
		productPanel.add( this.productPoidsLabel, new GridBagConstraints( 0, 1,
		        1, 1, 0, 1, GridBagConstraints.WEST, GridBagConstraints.NONE,
		        new Insets( 0, 0, 5, 5 ), 0, 0 ) );
		productPanel.add( this.productUnitList, new GridBagConstraints( 1, 1,
		        1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE,
		        new Insets( 0, 0, 5, 0 ), 0, 0 ) );

		/*
		 * JPanel imageOriginPanel = new JPanel( new GridBagLayout() );
		 * imageOriginPanel.add( this.imageOriginLabel, new GridBagConstraints(
		 * 0, 0, 4, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE,
		 * new Insets( 5, 0, 5, 0 ), 0, 0 ) ); imageOriginPanel.add(
		 * this.imageXOriginLabel, new GridBagConstraints( 0, 1, 1, 1, 0, 1,
		 * GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0,
		 * 5, 5 ), 0, 0 ) ); imageOriginPanel.add( this.imageXOriginSpinner, new
		 * GridBagConstraints( 1, 1, 1, 1, 0, 0, GridBagConstraints.WEST,
		 * GridBagConstraints.NONE, new Insets( 0, 0, 5, 10 ), -10, 0 ) );
		 * imageOriginPanel.add( this.imageYOriginLabel, new GridBagConstraints(
		 * 2, 1, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE,
		 * new Insets( 0, 0, 5, 5 ), 0, 0 ) ); imageOriginPanel.add(
		 * this.imageYOriginSpinner, new GridBagConstraints( 3, 1, 1, 1, 0, 0,
		 * GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0,
		 * 5, 0 ), -10, 0 ) ); imageOriginPanel.add(
		 * this.imageOriginPreviewComponent, new GridBagConstraints( 0, 2, 4, 1,
		 * 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(
		 * 5, 0, 5, 0 ), 0, 0 ) );
		 */

		add( CategoryInformationPanel );
		add( productPanel );
		/*
		 * add( imageOriginPanel, BackgroundImageWizardController.Step.ORIGIN
		 * .name() );
		 */
	}




	/**
	 * Creates components displayed by this panel.
	 */
	private void createComponents( UserPreferences preferences )
	{
		// Get unit text matching current unit
		String unitText = this.resource
		        .getString( preferences.getUnit() == UserPreferences.Unit.KILO ? "kiloUnit"
		                : "gallonUnit" );

		// Category choice panel components
		this.categoryNameLabel = new JLabel();
		this.categoryChangeButton = new JButton();
		this.categoryChangeButton.addActionListener( new ActionListener()
		{
			public void actionPerformed( ActionEvent ev )
			{
				/*
				 * Content content = showImageChoiceDialog(); if ( content !=
				 * null ) { updateComponents( content ); }
				 */
			}
		} );

		// Image scale panel components
		this.productNameLabel = new JLabel( this.resource
		        .getString( "productNameLabel.text" ) );
		this.productPoidsLabel = new JLabel( String.format( this.resource
		        .getString( "productPoidsLabel.text" ), unitText ) );
		this.productUnitList = new JList( new DefaultListModel() );
		this.productUnitList.getModel().addListDataListener(
		        new ListDataListener()
		        {
			        public void intervalAdded( ListDataEvent ev )
			        {
				        // If spinner value changes update controller
				        /*
						 * controller .setScaleDistance( ( (
						 * NullableSpinner.NullableSpinnerLengthModel )
						 * productUnitList .getModel() ).getLength() );
						 */
			        }




			        public void contentsChanged( ListDataEvent e )
			        {
				        // TODO Auto-generated method stub

			        }




			        public void intervalRemoved( ListDataEvent e )
			        {
				        // TODO Auto-generated method stub

			        }
		        } );
	}
}